package com.ny.util;

import com.ny.entity.Device;
import com.ny.mapper.DeviceRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

@Component
public class MongoDataInitializer {
    
    @Autowired
    private DeviceRepository deviceRepository;
    
    public void initializeData() {
        // 清空现有数据
        deviceRepository.deleteAll();
        
        // 创建新的设备数据
        List<Device> devices = Arrays.asList(
            createDevice(1, "客厅空调", 2, 1, 1),
            createDevice(2, "卧室吸顶灯", 1, 1, 2),
            createDevice(3, "厨房燃气传感器", 5, 1, 3),
            createDevice(4, "次卧窗帘电机", 4, 0, 4),
            createDevice(5, "客厅烟雾传感器", 5, 1, 1)
        );
        
        // 保存到MongoDB
        deviceRepository.saveAll(devices);
        System.out.println("MongoDB数据初始化完成，共插入 " + devices.size() + " 条记录");
    }
    
    public void clearAllData() {
        long count = deviceRepository.count();
        deviceRepository.deleteAll();
        System.out.println("已清除MongoDB中的 " + count + " 条设备记录");
    }
    
    public long getCount() {
        return deviceRepository.count();
    }
    
    private Device createDevice(Integer deviceId, String deviceName, Integer deviceType, Integer status, Integer roomId) {
        Device device = new Device();
        device.setDeviceId(deviceId);
        device.setDeviceName(deviceName);
        device.setDeviceType(deviceType);
        device.setStatus(status);
        device.setRoomId(roomId);
        return device;
    }
}